package com.fayou.cloud.seataorderservice2001.controller;

import com.fayou.cloud.seataorderservice2001.entity.OrderTbl;
import com.fayou.cloud.seataorderservice2001.service.OrderTblService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (OrderTbl)表控制层
 *
 * @author makejava
 * @since 2020-06-03 16:18:55
 */
@RestController
@RequestMapping("order")
public class OrderTblController {
    /**
     * 服务对象
     */
    @Resource
    private OrderTblService orderTblService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public OrderTbl selectOne(@RequestParam("id") Integer id) {
        return this.orderTblService.queryById(id);
    }


    /**
     * 下单：插入订单表、扣减库存，模拟回滚
     *
     * @return
     */
    @GetMapping("/placeOrder/commit")
    public Boolean placeOrderCommit() {

        orderTblService.placeOrder("1", "product-1", 1);
        return true;
    }

    /**
     * 下单：插入订单表、扣减库存，模拟回滚
     *
     * @return
     */
    @GetMapping("/placeOrder/rollback")
    public Boolean placeOrderRollback() {
        // product-2 扣库存时模拟了一个业务异常,
        orderTblService.placeOrder("1", "product-2", 1);
        return true;
    }


    @GetMapping("/placeOrder")
    public Boolean placeOrder(String userId, String commodityCode, Integer count) {
        orderTblService.placeOrder(userId, commodityCode, count);
        return true;
    }


    /**
     * 下单：插入订单表、扣减库存，模拟回滚
     *
     * @return
     */
    @GetMapping("/test")
    public String test() {
        return "test";
    }
}